package com.test.com.bluetoothchatandroid.adapter;

import android.animation.ValueAnimator;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.test.com.bluetoothchatandroid.R;
import com.test.com.bluetoothchatandroid.entity.ExpandDataBean;

import java.util.List;


/**
 * Created by ${cs} on 2018/4/16.
 * @author saviorT
 */

public class ExpandAdapter extends BaseExpandableListAdapter {

	/**
	 * android 官方推荐
	 * 当使用HashMap(K, V),如果K为整数类型时,使用SparseArray的效率更高。
	 */
	private List<ExpandDataBean> data;
	private Context mContext;

	public ExpandAdapter(Context context) {
		this.mContext = context;
	}

	@Override
	public int getGroupCount() {
		if (data == null || data.size() == 0){return 0;}
		return data.size();
	}

	@Override
	public int getChildrenCount(int i) {
		if (data == null || data.size() == 0 || data.get(i) == null || data.get(i).innerList.size() == 0){return 0;}
		return data.get(i).innerList.size();
	}

	@Override
	public Object getGroup(int i) {
		if (data == null || data.size() == 0){
			return null;
		}
		return data.get(i);
	}

	@Override
	public Object getChild(int i, int j) {
		if (data == null || data.size() == 0){
		return null;}
		return data.get(i).innerList.get(j);
	}

	@Override
	public long getGroupId(int i) {
		return 0;
	}

	@Override
	public long getChildId(int i, int i1) {
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	/**
	 *
	 * @param i position 所处位置
	 * @param isExpanded 是否展开
	 * @param convertView
	 * @param viewGroup
	 * @return
	 */
	@Override
	public View getGroupView(int i, boolean isExpanded, View convertView, ViewGroup viewGroup) {
		if (data == null || data.get(i)==null){
			return null;
		}
		final GroupViewHolder groupViewHolder;
		if (convertView == null){
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_expand_group_layout,null);
			groupViewHolder = new GroupViewHolder();
			groupViewHolder.titleTv = convertView.findViewById(R.id.group_title);
			groupViewHolder.relativeLayout = convertView.findViewById(R.id.itemRl);
			groupViewHolder.arrowIV = convertView.findViewById(R.id.arrowIv);
			convertView.setTag(groupViewHolder);
		}else {
			groupViewHolder = (GroupViewHolder) convertView.getTag();
		}

		ExpandDataBean bean = data.get(i);
		List<ExpandDataBean.ExpandInnerBean> list = bean.innerList;

		groupViewHolder.arrowIV.setSelected(isExpanded);
		groupViewHolder.titleTv.setText("标题："+bean.outerName+" 条目长度："+list.size());
		return convertView;
	}

	@Override
	public View getChildView(int i, int i1, boolean b, View convertView, ViewGroup viewGroup) {
		if (data == null
				|| data.get(i) == null ||data.get(i) == null
				|| data.get(i).innerList.size() == 0
				||data.get(i).innerList.get(i1) == null ){
			return null;
		}
		ItemViewHolder itemViewHolder = null;
		if (itemViewHolder == null){
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_expand_item_layout,null);
			itemViewHolder = new ItemViewHolder();
			itemViewHolder.nameTv = convertView.findViewById(R.id.nameTv);
			itemViewHolder.itemRl = convertView.findViewById(R.id.itemRl);
			convertView.setTag(itemViewHolder);
		}else {
			itemViewHolder = (ItemViewHolder) convertView.getTag();
		}
		ExpandDataBean.ExpandInnerBean bean = data.get(i).innerList.get(i1);
		itemViewHolder.nameTv.setText(bean.innerName);
		itemViewHolder.itemRl.setOnClickListener(view -> {
			Toast.makeText(mContext,String.format("第%d个Group的第%d个Item被点击",i,i1),Toast.LENGTH_SHORT).show();
		});
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int i, int i1) {
		return false;
	}

	public void setGroupInfoList(List<ExpandDataBean> data){
		this.data = data;
		this.notifyDataSetChanged();
	}

	class GroupViewHolder{
		TextView titleTv;
		RelativeLayout relativeLayout;
		ImageView arrowIV;
	}
	class ItemViewHolder{
		TextView nameTv;
		RelativeLayout itemRl;
	}

	private void  show(final View view ,int height){
		view.setVisibility(View.VISIBLE);
		ValueAnimator animator = ValueAnimator.ofInt(0,height);
		animator.setDuration(500);
		animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				int value = (Integer) animation.getAnimatedValue();
				view.getLayoutParams().height = value;
				view.setLayoutParams(view.getLayoutParams());
			}
		});
		animator.start();
	}

	private void dismiss(final View v, int height){
		ValueAnimator animator = ValueAnimator.ofInt(height,0);
		animator.setDuration(500);
		animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				int value = (Integer) animation.getAnimatedValue();
				if (value == 0) {
					v.setVisibility(View.GONE);
				}
				v.getLayoutParams().height = value;
				v.setLayoutParams(v.getLayoutParams());
			}
		});
		animator.start();
	}
}
